from py2neo import Graph, NodeMatcher, RelationshipMatcher

# 连接到Neo4j数据库
graph = Graph("http://localhost:7474", auth=("neo4j", "123456"))

# 导出节点和关系的内容
def export_data():
    # 打开文件
    with open("1.txt", "w") as file:
        node_matcher = NodeMatcher(graph)
        relationship_matcher = RelationshipMatcher(graph)

        # 导出节点
        nodes = node_matcher.match()
        for node in nodes:
            file.write("%s" % str(node))

        # 导出关系
        relationships = relationship_matcher.match()
        for relationship in relationships:
            # print(str(relationship))
            file.write(str(relationship))

# 导出数据
export_data()



# from py2neo import Graph

# # 连接到Neo4j数据库
# graph = Graph("http://localhost:7474", auth=("neo4j", "123456"))

# # 导出节点和关系的内容，并将关系写入文件
# def export_data():
#     # 打开文件
#     with open("1.txt", "w") as file:
#         relationship_list = []
        
#         # 导出节点
#         nodes = graph.nodes
#         for node in nodes:
#             file.write("%s" % node)

#         # 导出关系
#         relationships = graph.relationships
#         for relationship in relationships:
#             relationship_list.append(str(relationship))

#         # 写入关系到文件
#         file.write("\n".join(relationship_list))

# # 导出数据
# export_data()